<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/16
 * Time: 9:52
 */

namespace app\api\controller;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,fs-token");
header('Access-Control-Allow-Methods: GET, POST');

use app\api\validate\Authentication;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\LoginByPhone;
use app\lib\expection\UserException;
use app\api\model\Captcha;
use app\api\model\User as UserModel;
/**
 * @title 用户管理接口
 * @group app
 */
class User extends Base
{
    /**
     * @title 手机号登陆
     * @author 开发者
     * @url /api/user/loginByPhone
     * @method POST
     * @param name:phone type:char require:1 default:null  desc:手机号
     * @param name:captcha type:varchar require:1 default:null  desc:短信验证码
     *  @return id:用户id
     * @return username:用户名
     * @return phone:手机号
     * @return img:头像
     * @return email:邮箱
     * @return school:学校
     * @return gender:性别 1男 2女
     * @return last_login_time:最后的登录时间
     */
    public function loginByPhone(){
        //获得提交的手机号和密码
        $phone = input('phone');
        $captcha=input('captcha');
        //手机号登陆验证
        (new LoginByPhone())->goCheck();
        //查找验证码信息
        $data = Captcha::getUserInfoByPhone($phone);
        if ($captcha!= $data['captcha']) {
            $this->apiReturn(303, '验证码输入错误，请重新输入！！！');
        }
        //验证码过期时间
        $over_time = $data['add_time'] + 6000;
        $time = time();
        if ($time > $over_time) {
            $this->apiReturn(303, '验证码已经过期，请重新获得验证码！！！');
        }
        $data->user->last_login_time=$time;
        $data->user->save();
        //返回用户的相关信息
        $this->apiReturn(200,'登录成功',$data->user);
    }
    /**
     * @title 手机号注册
     * @author 开发者
     * @url /api/user/registerByPhone
     * @method POST
     * @param name:phone type:char require:1 default:null  desc:手机号
     * @param name:captcha type:varchar require:1 default:null  desc:短信验证码
     */
    public function registerByPhone(){
        //获得提交的手机号和密码
        $phone = input('phone');
        $captcha=input('captcha');
        //手机号登陆验证
        (new LoginByPhone())->goCheck();
        //查找验证码信息
        $data = Captcha::getUserInfoByPhone($phone);
        if ($captcha!= $data['captcha']) {
            $this->apiReturn(303, '验证码输入错误，请重新输入！！！');
        }
        //验证码过期时间
        $over_time = $data['add_time'] + 600;
        $time = time();
        if ($time > $over_time) {
            $this->apiReturn(303, '验证码已经过期，请重新获得验证码！！！');
        }
        //判断用户是否已经存在
       if($data->user){
           $this->apiReturn(303,'手机号已被注册，请直接登陆！');
       }
        $data = [
            'phone' => $phone,
        ];
       //将手机号保存至数据库
        $result=new UserModel();
        if($result->save($data)){
            //返回用户的相关信息
            $this->apiReturn(200,'注册成功');
        }else{
            $this->apiReturn(303,'注册失败');
        }
    }

    /**
     * @title 用户验证
     * @author 开发者
     * @url /api/user/authentication
     * @method POST
     * @param name:img type:varchar require:1 default:images/avatar/1.png  desc:头像地址
     * @param name:username type:varchar require:1 default:null  desc:姓名
     * @param name:school type:varchar require:1 default:null  desc:学校
     * @param name:user_id type:int require:1 default:null  desc:用户id
     */
    public  function authentication(){
        //接受数据
        $data = I('img|username|school|user_id');
        //对数据进行验证
        (new Authentication())->goCheck();
        $user_id =$data['user_id'];
        unset($data['user_id']);
        $result=UserModel::authentication($data,$user_id);
        if($result){
            $this->apiReturn(200,'认证成功!');
        }else{
            $this->apiReturn(303,'认证失败！');
        }
    }

}